#include "StdAfx.h"
#include "DAnimation.h"

DAnimation::DAnimation(void)
: m_bSingleFrame(false)
, m_bLoop(true)
, m_nCurrentFrame(0)
, m_nWidth(0)
, m_nHeight(0)
, m_fSpeed(14.0f)
{
}

DAnimation::~DAnimation(void)
{
}

bool DAnimation::isSingleFrame() const
{
	return m_bSingleFrame;
}

bool DAnimation::isLoop() const
{
	return m_bLoop;
}
void DAnimation::setLoop(bool bLoop /*= true*/)
{
	m_bLoop = bLoop;
}

boost::shared_ptr<DImage> DAnimation::currentFrame() const
{
	return m_frames[m_nCurrentFrame].lock();
}

bool DAnimation::update(float dt)
{
	static float speedTimer = 0.0f;
	speedTimer += dt;

	if(speedTimer > 1.0f/m_fSpeed)
	{
		++m_nCurrentFrame;
		speedTimer = 0;
	}

	if(m_nCurrentFrame < (int)m_frames.size())
	{
		// do nothing
	}
	else if(m_bLoop)
	{
		m_nCurrentFrame = 0;
	}
	else
	{
		return true;
	}

	// not finished
	return false;
}

void DAnimation::addFrame(boost::shared_ptr<DImage> frame)
{
	m_frames.push_back(frame);
}

unsigned int DAnimation::width() const
{
    return m_nWidth;
}

unsigned int DAnimation::height() const
{
    return m_nHeight;
}

void DAnimation::setDimensions(unsigned int w, unsigned int h)
{
    m_nWidth = w;
    m_nHeight = h;
}

void DAnimation::setSingleFrame(bool bSingleFrame /*= true*/)
{
	m_bSingleFrame = bSingleFrame;
}